/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;

namespace Xceed.Silverlight.Data
{
  public sealed class FilterExpressionCollection : IList<FilterExpressionBase>, IList
  {
    #region Constructor

    internal FilterExpressionCollection( FilterExpressionOperator filterExpressionOperator )
    {
      if( filterExpressionOperator == null )
        throw new ArgumentNullException( "filterExpressionOperator" );

      m_parentFilterExpressionOperator = filterExpressionOperator;
    }

    #endregion

    internal void InternalAdd( FilterExpressionBase expression )
    {
      m_innerList.Add( expression );
    }

    #region IList<FilterExpressionBase> Members

    public int IndexOf( FilterExpressionBase item )
    {
      return m_innerList.IndexOf( item );
    }

    public void Insert( int index, FilterExpressionBase item )
    {
      if( m_parentFilterExpressionOperator.IsFrozen )
        throw new InvalidOperationException( "An attempt was made to modify the collection while the FilterExpressionOperator is frozen." );

      m_innerList.Insert( index, item );
    }

    public void RemoveAt( int index )
    {
      if( m_parentFilterExpressionOperator.IsFrozen )
        throw new InvalidOperationException( "An attempt was made to modify the collection while the FilterExpressionOperator is frozen." );

      m_innerList.RemoveAt( index );
    }

    public FilterExpressionBase this[ int index ]
    {
      get
      {
        return m_innerList[ index ];
      }
      set
      {
        if( m_parentFilterExpressionOperator.IsFrozen )
          throw new InvalidOperationException( "An attempt was made to modify the collection while the FilterExpressionOperator is frozen." );

        m_innerList[ index ] = value;
      }
    }

    #endregion

    #region ICollection<FilterExpressionBase> Members

    public void Add( FilterExpressionBase item )
    {
      if( m_parentFilterExpressionOperator.IsFrozen )
        throw new InvalidOperationException( "An attempt was made to modify the collection while the FilterExpressionOperator is frozen." );

      m_innerList.Add( item );
    }

    public void Clear()
    {
      if( m_parentFilterExpressionOperator.IsFrozen )
        throw new InvalidOperationException( "An attempt was made to modify the collection while the FilterExpressionOperator is frozen." );

      m_innerList.Clear();
    }

    public bool Contains( FilterExpressionBase item )
    {
      return m_innerList.Contains( item );
    }

    public void CopyTo( FilterExpressionBase[] array, int arrayIndex )
    {
      m_innerList.CopyTo( array, arrayIndex );
    }

    public int Count
    {
      get
      {
        return m_innerList.Count;
      }
    }

    public bool IsReadOnly
    {
      get
      {
        if( m_parentFilterExpressionOperator.IsFrozen )
          return true;

        return false;
      }
    }

    public bool Remove( FilterExpressionBase item )
    {
      if( m_parentFilterExpressionOperator.IsFrozen )
        throw new InvalidOperationException( "An attempt was made to modify the collection while the FilterExpressionOperator is frozen." );

      return m_innerList.Remove( item );
    }

    #endregion

    #region IEnumerable<FilterExpressionBase> Members

    public IEnumerator<FilterExpressionBase> GetEnumerator()
    {
      return m_innerList.GetEnumerator();
    }

    #endregion

    #region IEnumerable Members

    IEnumerator IEnumerable.GetEnumerator()
    {
      return ( ( IEnumerable )m_innerList ).GetEnumerator();
    }

    #endregion

    #region IList Members

    int IList.Add( object value )
    {
      if( m_parentFilterExpressionOperator.IsFrozen )
        throw new InvalidOperationException( "An attempt was made to modify the collection while the FilterExpressionOperator is frozen." );

      return ( ( IList )m_innerList ).Add( value as FilterExpressionBase );
    }

    bool IList.Contains( object value )
    {
      return this.Contains( value as FilterExpressionBase );
    }

    int IList.IndexOf( object value )
    {
      return this.IndexOf( value as FilterExpressionBase );
    }

    void IList.Insert( int index, object value )
    {
      this.Insert( index, value as FilterExpressionBase );
    }

    bool IList.IsFixedSize
    {
      get
      {
        return ( ( IList )m_innerList ).IsFixedSize;
      }
    }

    void IList.Remove( object value )
    {
      this.Remove( value as FilterExpressionBase );
    }

    object IList.this[ int index ]
    {
      get
      {
        return this[ index ];
      }
      set
      {
        this[ index ] = value as FilterExpressionBase;
      }
    }

    #endregion

    #region ICollection Members

    void ICollection.CopyTo( Array array, int index )
    {
      throw new NotSupportedException( "ICollection.CopyTo() is not supported, use ICollection<FilterExpressionBase>.CopyTo() instead." );
    }


    bool ICollection.IsSynchronized
    {
      get
      {
        return ( ( ICollection )m_innerList ).IsSynchronized;
      }
    }

    object ICollection.SyncRoot
    {
      get
      {
        return ( ( ICollection )m_innerList ).SyncRoot;
      }
    }

    #endregion

    #region Private Fields

    private List<FilterExpressionBase> m_innerList = new List<FilterExpressionBase>();
    private FilterExpressionOperator m_parentFilterExpressionOperator; //null

    #endregion
  }
}
